package top.defaults.colorpicker;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.LinearShader;
import ohos.agp.render.Paint;
import ohos.agp.render.Shader;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.app.Context;
import top.defaults.colorpicker.util.Constant;
import top.defaults.colorpicker.util.NumCalcUtil;

import java.math.BigDecimal;

/**
 * 底部亮度调节view
 *
 * @author jjy
 * @since 2021-05-10
 */
public class AlphaSliderView extends ColorSliderView implements Component.DrawTask {
    private int width;
    private int height;

    /**
     * 构造函数
     *
     * @param context context对象
     */
    public AlphaSliderView(Context context) {
        super(context);
    }

    /**
     * 构造函数
     *
     * @param context context对象
     * @param attrs 属性值
     */
    public AlphaSliderView(Context context, AttrSet attrs) {
        super(context, attrs);
        invalidate();
    }

    /**
     * 构造函数
     *
     * @param context context对象
     * @param attrs 属性值
     * @param defStyleAttr 样式值
     */
    public AlphaSliderView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void invalidate() {
        super.invalidate();
        this.addDrawTask(this::onDraw);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        width = getWidth();
        height = getHeight();
        onSizeChanged(width, height);
        drawAlphaBackground(canvas);
        super.onDraw(component, canvas);
    }

    private void drawAlphaBackground(Canvas canvas) {
        Paint bgPaint = new Paint();
        bgPaint.setColor(Color.GRAY);
        int rectSize = (int)NumCalcUtil.subtract(height, (float)selectorSize);
        for (int index = 0; index < NumCalcUtil.divide(NumCalcUtil.subtract(width, (float)selectorSize), rectSize); index += Constant.NUM_2) {
            canvas.drawRect(NumCalcUtil.add((float)selectorSize, index * rectSize), (float)selectorSize,
                NumCalcUtil.add(NumCalcUtil.add((float)selectorSize, index * rectSize), rectSize), height, bgPaint);
        }
    }

    @Override
    protected float resolveValue(int color) {
        return Color.alpha(color) / Constant.ALPHA_TOP_VALUE;
    }

    /**
     * 配置画笔
     *
     * @param colorPaint 画笔对象
     */
    protected void configurePaint(Paint colorPaint) {
        int[] baseRgbColors = ColorConverUtils.colorToRgb(baseColor);
        int startColor = Color.argb(0, baseRgbColors[0], baseRgbColors[1], baseRgbColors[Constant.NUM_2]);
        int endColor = Color.argb((int)Constant.ALPHA_TOP_VALUE,
            baseRgbColors[0], baseRgbColors[1], baseRgbColors[Constant.NUM_2]);

        Color[] colors = new Color[]{new Color(startColor), new Color(endColor)};
        Point[] pointArrays = new Point[]{new Point(0, 0),
            new Point(width, height)};
        Shader shader = new LinearShader(pointArrays,
            null, colors, Shader.TileMode.CLAMP_TILEMODE);
        colorPaint.setShader(shader, Paint.ShaderType.LINEAR_SHADER);
    }

    /**
     * 获取基础颜色
     *
     * @return 基础颜色
     */
    protected int assembleColor() {
        int[] baseRgbColors = ColorConverUtils.colorToRgb(baseColor);
        int alpha = (int) (currentValue * (int)Constant.ALPHA_TOP_VALUE);
        int startColor = Color.argb(alpha, baseRgbColors[0], baseRgbColors[1], baseRgbColors[Constant.NUM_2]);
        return startColor;
    }
}
